require('es6-promise').polyfill();

import axios from 'axios'

function createPropertySysRoleData(data){
  return {
    type:'CREATE_PROPERTY_SYSROLE_DATA',
    data,
    date:Date.now()
  };
}

function loadSysRoleData(data) {
  return {
    type: 'LOAD_PROPERTY_SYSROLE_DATA',
    data
  }
}

function loadCorpData(data) {
  return {
    type: 'LOAD_COPR_SYSROLE_DATA',
    data
  }
}

export function createSysRoleDataAsync(data) {
  data.userType = 'PROPERTY';
  return function(dispatch) {
    return axios.post('/api/sysuser/create',data).then(
      resp => dispatch(loadSysRoleDataAsync({})),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function createCorpDataAsync(data) {
  data.userType = 'CORP';
  return function(dispatch) {
    return axios.post('/api/sysuser/create',data).then(
      resp => dispatch(loadCorpDataAsync({})),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function editCorpDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysuser/update',data).then(
      resp => dispatch(loadCorpDataAsync({})),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function editSysRoleDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysuser/update',data).then(
      resp => dispatch(loadSysRoleDataAsync(data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}


export function deleteSysRoleDataAsync(data){
  return function(dispatch) {
    return axios.post('/api/sysuser/delete',data).then(
      resp => dispatch(loadSysRoleDataAsync({})),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function deleteCorpDataAsync(data){
  return function(dispatch) {
    return axios.post('/api/sysuser/delete',data).then(
      resp => dispatch(loadCorpDataAsync({})),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}



export function loadSysRoleDataAsync(data) {
  data.userType = 'PROPERTY';
  return function(dispatch) {
    return axios.post('/api/sysuser/querySysUser',data).then(
      resp => dispatch(loadSysRoleData(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function loadCorpDataAsync(data) {
  data.userType = 'CORP';
  return function(dispatch) {
    return axios.post('/api/sysuser/querySysUser',data).then(
      resp => dispatch(loadCorpData(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function addSysRoleUserAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysroleuser/updateRoles',data).then(
      resp => console.log(resp.data),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function deleteSysRoleUserAsync(data) {
  // console.log(data)
  return function(dispatch) {
    return axios.post('/api/sysroleuser/deleteRoles',data).then(
      resp => console.log(resp.data),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function switchRoleUserAsync(data){
  return function(dispatch) {
    return axios.post('/api/sysuser/active',data).then(
      resp => dispatch(loadSysRoleDataAsync(data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}
